<?php

namespace backend\models\service;

use Yii;
use backend\models\odm\CateOdm;

class CateService extends CateOdm {

    private static $_items = array();

    public static function item($type) {
        if (!isset(self::$_items[$type]))
            self::loadItems($type);
        return self::$_items[$type];
    }

    public static function items($type, $is_all = 'all') {
        if (!isset(self::$_items[$type]))
            self::loadItems($type, $is_all);
        array_unshift(self::$_items[$type], '请选择');
        return self::$_items[$type];
    }

    private static function loadItems($type, $is_all = 'no') {
        self::$_items[$type] = [];
        if ($is_all == 'no') {
            $model = static::find()->where(['cate_id' => $type])->asArray()->One();
            self::$_items[$type] = $model['cate_name'];
        } else {
            $models = static::find()->orderBy(['cate_id' => SORT_ASC])->asArray()->all();
            foreach ($models as $model) {
                self::$_items[$type][$model['cate_id']] = $model['cate_name'];
            }
        }
    }
    
    
    public static function getCate($type){
        self::$_items = [];
        $data = static::find()->where(['cate_main' => $type])->asArray()->all();
        foreach ($data as $model) {
            self::$_items[$model['cate_id']] = $model['cate_name'];
        }
        return self::$_items;
    }
    
    
    public function getCateData($cate_main){
        $sql = "select cate_id,cate_main,cate_name from xs_novel_category where cate_main = ".$cate_main." order by cate_sort asc";
        $datas = Yii::$app->db->createCommand($sql)->queryAll();
        return $datas;
    }
    
    public function getMainCate($cid) {
        $sql = "select cate_id,cate_main,cate_name from xs_novel_category where cate_id = " . $cid;
        $datas = Yii::$app->db->createCommand($sql)->queryOne();
        return $datas;
    }     


}
